package com.creative.accountsdk;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;

import com.creative.accountsdk.holder.BaseHolder;
import com.creative.accountsdk.holder.BindAcountHolder;
import com.creative.accountsdk.holder.BindWawayayaHolder;
import com.creative.accountsdk.holder.ChildAddClassHolder;
import com.creative.accountsdk.holder.ChildClassInfoHolder;
import com.creative.accountsdk.holder.ChildUnbindParentHolder;
import com.creative.accountsdk.holder.FindPasswordHolder;
import com.creative.accountsdk.holder.LoginHolder;
import com.creative.accountsdk.holder.ModifyPasswordHolder;
import com.creative.accountsdk.holder.MyInviteCodeHolder;
import com.creative.accountsdk.holder.RegisteHolder;
import com.creative.accountsdk.holder.UserInfoHolder;
import com.creative.accountsdk.holder.Constants;
import com.creative.accountsdk.teacher.ClassListActivity;

import java.util.ArrayList;
import java.util.List;


public class AccountActivity extends ThirdLoginActivity {
    private ViewGroup layoutRoot;
    private ImageView  btnCancel;
    private List<BaseHolder> holderList;
    private Handler handler;
    private final static int MSG_BIGGER = 1;
    private final static int MSG_SMALLER = 2;

    public  UserInfoHolder  userInfoHolder;
    public  BindAcountHolder  bindAccountHolder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置无标题
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //设置全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        DataCenter.init(this);
        Log.d(getClass().getSimpleName(), "onCreate");
   /*     int orientation = AccountSdk.getInstance().getOrientation();
        if(orientation == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)
        {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }
        else {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }*/
        setContentView(R.layout.acount_main_layout);

        holderList = new ArrayList<>();
        layoutRoot = (ViewGroup)findViewById(R.id.layoutContent);
        layoutRoot.removeAllViews();
        btnCancel = (ImageView)findViewById(R.id.exitImgBtn);
        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AccountActivity.this.finish();
            }
        });

        if(getIntent().hasExtra("flag"))
        {
            switch (getIntent().getStringExtra("flag"))
            {
                case Constants.BIND_WAWAYAYA:
                    showHolder(new BindWawayayaHolder());
                    break;
                case Constants.MODIFY_PASSWORD:
                    showHolder(new ModifyPasswordHolder());
                    break;
                case Constants.FIND_PASSWORD:
                    showHolder(new FindPasswordHolder());
                    break;
                case Constants.REGISTER:
                    showHolder(new RegisteHolder());
                    break;
                case Constants.USERINFO:
                    userInfoHolder = new UserInfoHolder(this);
                    showHolder(userInfoHolder);
                    break;
                case Constants.MY_INVITE_CODE:
                    showHolder(new MyInviteCodeHolder());
                    break;

                case Constants.PARENT_BIND_CHILD_ACCOUNT:
                    bindAccountHolder = new BindAcountHolder(true);
                    showHolder(bindAccountHolder);
                    break;
                case Constants.CHILD_BIND_PARENT_ACCOUNT:
                    bindAccountHolder = new BindAcountHolder(false);
                    showHolder(bindAccountHolder);
                break;

                case Constants.BIND_ACCOUNT:
                    if (UserData.getInstance().getRole() == 1) {
                        bindAccountHolder = new BindAcountHolder(true);
                        showHolder(bindAccountHolder);
                    } else if (UserData.getInstance().getRole() == 2 ) {
                        bindAccountHolder = new BindAcountHolder(false);
                        showHolder(bindAccountHolder);
                    }
                    break;
                case Constants.UNBIND_ACCOUNT:
                    UserInfor parentInfo = UserData.getInstance().getParentinfor();
                    if (parentInfo != null) {
                        String parentName = !TextUtils.isEmpty(parentInfo.nickname)? parentInfo.nickname : parentInfo.realname;
                        showHolder(new ChildUnbindParentHolder(parentName));
                    } else {
                        finish();
                    }
                    break;
                case Constants.CHILD_ADD_CLASS:
                    showHolder(new ChildAddClassHolder());
                    break;
                case Constants.CLASS_LIST:
                    toClassList();
                    break;
                case Constants.CLASS_INFO:
                    ClassInfo classinfo = UserData.getInstance().getClassinfor();
                    if (classinfo != null) {
                        showHolder(new ChildClassInfoHolder(classinfo.className));
                    }  else {
                        finish();
                    }
                    break;
                default:
                    showHolder(new LoginHolder());
                    break;
            }
        }
        else {
            showHolder(new LoginHolder());
        }

    }

    public void showHolder(BaseHolder holder)
    {
        ViewGroup viewRoot = (ViewGroup)getLayoutInflater().inflate(holder.getLayoutResID(), layoutRoot, false);
        layoutRoot.addView(viewRoot);
        holder.init(this, viewRoot);
        holderList.add(holder);
    }

    public void popupHolder()
    {
        if(holderList.size() > 0)
        {
            BaseHolder holder = holderList.remove(holderList.size()-1);
            layoutRoot.removeView(holder.getViewRoot());
        }
    }

    public void hideHolder()
    {
        if(holderList.size() > 0)
        {
            BaseHolder holder = holderList.get(holderList.size() - 1);
            holder.getViewRoot().setVisibility(View.GONE);
        }
    }

    public void visibleHolder() {
        if (holderList.size() > 0)
        {
            BaseHolder holder = holderList.get(holderList.size() - 1);
            holder.getViewRoot().setVisibility(View.VISIBLE);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK)
        {
            OnLoginListener loginListener = AccountSdk.getInstance().getLoginListener();
            if (loginListener != null) {
                loginListener.onCancel();
            }
            return super.onKeyDown(keyCode, event);
        }
        return super.onKeyDown(keyCode, event);
    }

    public void hideSoftInput()
    {
        if(getCurrentFocus() != null) {
            InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
        }
    }

    private int dip2px(float dp)
    {
        float density = getResources().getDisplayMetrics().density;
        return (int)(dp*density+0.5f);
    }

    private void  toClassList(){
        Intent intent = new Intent(this, ClassListActivity.class);
        startActivity(intent);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (RESULT_OK == resultCode) {
            if (requestCode == Constants.REQUEST_CHOOSE_PICTURE) {
                if (userInfoHolder != null) {
                    userInfoHolder.chooseCropPicture(data.getData());
                }
            } else if (requestCode == Constants.REQUEST_TAKE_PICTURE) {
                if (userInfoHolder != null) {
                    userInfoHolder.cropPicture();
                }
            }else if (requestCode == Constants.REQUEST_CHOOSE_CROP_PICTURE) {

                if (userInfoHolder != null) {
                    userInfoHolder.setChoosePicture(data.getData());
                }
            }else if(requestCode == Constants.REQUEST_TAKE_CROP_PICTURE){

                if (userInfoHolder != null) {
                    userInfoHolder.setTakePicture();
                }
            } else if (requestCode == Constants.REQUEST_CODE_SCAN) {
                if (bindAccountHolder != null) {
                    String retString = data.getStringExtra("result");
                    bindAccountHolder.setInviteCodeFromScan(retString);

                }

            }
        }
    }
}
